package cn.itcast.controller;

import cn.itcast.domain.User;
import cn.itcast.service.UserService;
import cn.itcast.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/user")
public class UserServlet extends HttpServlet {
    private UserService service = new UserServiceImpl();

    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if ("findAll".equals(method)) {
            findAll(req, resp);
        }
        if ("add".equals(method)) {
            add(req, resp);
        }
        if ("delete".equals(method)) {
            delete(req, resp);
        }
        if ("findById".equals(method)) {
            findById(req, resp);
        }
        if ("update".equals(method)){
            update(req,resp);
        }
        if ("deleteByIds".equals(method)){

            deleteByIds(req,resp);
        }

    }
    //批量删除
    protected void deleteByIds(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取页面数据
        String ids = req.getParameter("ids");

        Integer i =  service.deleteByIds(ids);
        if (i != null){
            req.getRequestDispatcher("/user?method=findAll").forward(req,resp);
        }else{
            resp.getWriter().write("删除失败");
        }

    }
    //修改数据
    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取页面数据
        String uid = req.getParameter("id");
        int id = Integer.parseInt(uid);

        String username = req.getParameter("username");
        String email = req.getParameter("email");
        //调用service 修改
        Integer i =  service.update(id,username,email);
        //重新查询数据库,遍历
        if (i!=null){
            req.getRequestDispatcher("/user?method=findAll").forward(req, resp);
        }
    }
    //数据回显
    protected void findById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取页面数据
        String uid = req.getParameter("uid");
        System.out.println(uid+"###########################");
       //调用service 查询
        User user  = service.findById(uid);
        System.out.println( user +"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
        //存到域中,跳转到update页面
        req.setAttribute("user",user);
        req.getRequestDispatcher("pages/User_Update.jsp").forward(req,resp);
    }

    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取页面数据
        String uid = req.getParameter("uid");
        //调用service删除
        Integer i = service.delete(uid);
        if (i !=null){
            req.getRequestDispatcher("/user?method=findAll").forward(req, resp);
        }
    }

    protected void findAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //调用service 查询所有
        List<User> userList = service.findAll();

        //将数据存到域中返回到list页面
        req.setAttribute("userList", userList);
        req.getRequestDispatcher("pages/User_List.jsp").forward(req, resp);
    }

    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取页面数据
        String username = req.getParameter("username");
        String email = req.getParameter("email");

        Integer i = service.add(username, email);
        if (i != null) {
            req.getRequestDispatcher("/user?method=findAll").forward(req, resp);
        }

    }
}
